package com.hnkywl.sts.action.zjgl;

import com.hnkywl.sts.entity.sys.Gncz;
import com.hnkywl.sts.entity.zjgl.Jgzs;
import com.hnkywl.sts.service.sys.GnczService;
import com.hnkywl.sts.service.zjgl.JgzsService;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by HL on 2015/9/18.
 */
@Namespace("/zjgl/jgzs")
public class JgzsAction extends SimpleActionSupport {

private JgzsService jgzsService;
    private int page;
    private int rows;
    private Jgzs jgzs;
    private Long jgzsId;
    //远程ajax验证的值
    private String validateVal;
    //需要远程ajax验证的属性
    private String validateField;
    private Date sysdate;
    //功能操作
    private List<Gncz> gnczList;
    private GnczService gnczService;


    @Action("jgzs")
    public String jgzs() {
        gnczList = gnczService.findGnczByJsAndUrl(request.getServletPath());
        return SUCCESS;
    }

    @Action("ajax_validate")
    public void ajaxValidate() {
        if (!jgzsService.ajaxValidate(jgzsId, validateField, validateVal))
            renderJson("{\"valid\":true}");
        else
            renderJson("{\"valid\":false}");
    }

    @Action("jgzsList")
    public void jgzsList() {
        this.renderJson(this.jgzsService.findAllJgzs(page, rows));
    }

    @Action("jgzs_content")
    public String jgzsContent() {
        if (jgzsId != null){
            jgzs = jgzsService.findJgzsDetail(jgzsId);
        }else{
            //进入add页面
            jgzs = new Jgzs();
        /*    sysdate = new Date(); //当前日期
            Calendar calendar = Calendar.getInstance();//日历对象
            calendar.setTime(sysdate);//设置当前日期*/
        }
        return SUCCESS;
    }



    @Action("add_jgzs")
    public void addFwpz() {
        try {
            this.jgzsService.saveJgzs(jgzs);
            renderText("{\"statusCode\":\"200\", \"message\":\"添加成功！\", \"operationType\":\"add\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }


    @Action("update_jgzs")
    public void updatejgzs() {
        try {
            this.jgzsService.updatejgzs(jgzs);
            renderText("{\"statusCode\":\"200\",\"message\":\"修改成功！\"}");
        } catch (RuntimeException e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }


    @Action("delete_jgzs")
    public void deletejgzs() {
        try {
            this.jgzsService.deletejgzs(jgzsId);
            renderText("{\"statusCode\":\"200\", \"message\":\"删除成功！\", \"operationType\":\"delete\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

 /*   @Action("save_jgzs")
    public void save(){
        try{
            jgzsService.save(jgzs);
            renderJson("{\"statusCode\":200}");
        }catch (Exception  e) {
            e.printStackTrace();
            renderJson("{\"statusCode\":300,\"message\":\""+e.getMessage()+"\"}");
        }
    }


    @Action("add_jgzs")
    public void addPjgl() {
        try {
            this.jgzsService.saveJgzs(jgzs);
            renderText("{\"statusCode\":\"200\", \"message\":\"新增成功！\", \"operationType\":\"add\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }*/
  //==========


    public Date getSysdate() {
        return sysdate;
    }

    public void setSysdate(Date sysdate) {
        this.sysdate = sysdate;
    }

    public String getValidateField() {
        return validateField;
    }

    public void setValidateField(String validateField) {
        this.validateField = validateField;
    }

    public String getValidateVal() {
        return validateVal;
    }

    public void setValidateVal(String validateVal) {
        this.validateVal = validateVal;
    }

    public Long getJgzsId() {
        return jgzsId;
    }

    public void setJgzsId(Long jgzsId) {
        this.jgzsId = jgzsId;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public Jgzs getJgzs() {
        return jgzs;
    }

    public void setJgzs(Jgzs jgzs) {
        this.jgzs = jgzs;
    }

    public List<Gncz> getGnczList() {
        return gnczList;
    }

    public void setGnczList(List<Gncz> gnczList) {
        this.gnczList = gnczList;
    }

    @Autowired
    public void setJgzsService(JgzsService jgzsService) {
        this.jgzsService = jgzsService;
    }

    @Autowired
    public void setGnczService(GnczService gnczService) {
        this.gnczService = gnczService;
    }
}
